//
//  PZTipsNullAlertView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/3/2.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "PZTipsNullAlertView.h"

@interface PZTipsNullAlertView()

{
    CGSize  _contentSize;//内容视图
    CGFloat _paddingTop, _paddingBottom, _paddingLeft; // paddingRight = paddingLeft   上下左右间距
    CGFloat _spacing;//共同间距
}

@end

@implementation PZTipsNullAlertView

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message constantWidth:(CGFloat)constantWidth
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        self.clipsToBounds = NO;
        _contentSize.width = 200; // 默认宽度
        if (constantWidth > 0) _contentSize.width = constantWidth;
        _paddingTop = 15;
        _paddingBottom = 15;
        _paddingLeft = 20;
        _spacing = 10;
        
        //标题
        if (title.length) {
            _titleLabel = [[UILabel alloc] init];
            _titleLabel.text = title;
            _titleLabel.numberOfLines = 0;
            _titleLabel.textAlignment = NSTextAlignmentCenter;
            _titleLabel.font = [UIFont systemFontOfSize:font20ScreenWRate * LKScreenW];
            [self addSubview:_titleLabel];
            
            _titleLabel.size = [_titleLabel sizeThatFits:CGSizeMake(_contentSize.width-2*_paddingLeft, MAXFLOAT)];
            _titleLabel.y = _paddingTop;
            _titleLabel.centerX = _contentSize.width / 2;
            _contentSize.height = _titleLabel.bottom;
            
            //标题及内容分割线
            _line = [UILabel new];
            _line.backgroundColor = NumberColor(236, 236, 236);
            [self addSubview:_line];
            _line.frame = CGRectMake(rate375(22),46, (_contentSize.width - 2*rate375(22)), 1);
            _contentSize.height = _line.bottom;
        }
        
        if (message.length) {
            _messageLabel = [[UILabel alloc] init];
            _messageLabel.numberOfLines = 0;
            _messageLabel.font = [UIFont systemFontOfSize:font15ScreenWRate * LKScreenW];
            _messageLabel.textColor = [UIColor colorWithHexString:battleshipGrey];
            
            [self addSubview:_messageLabel];
            
            NSAttributedString *string = [NSString createAttriStringWithString:message andSpace:2.0 Font:[UIFont systemFontOfSize:font15ScreenWRate * LKScreenW]];
            _messageLabel.attributedText = string;
            
            // _messageLabel.size = [_messageLabel sizeThatFits:CGSizeMake(_contentSize.width - 2*_paddingLeft, MAXFLOAT)];
            CGFloat Height =   [NSString getHeightWithText:message font:[UIFont systemFontOfSize:font15ScreenWRate * LKScreenW] MaxWidth:_contentSize.width - 2*_paddingLeft lineSpace:2];
            
            if (Height < 22) {
                //一行文字居中显示
                _messageLabel.textAlignment = NSTextAlignmentCenter;
            }else
            {
                //多行左对齐
                _messageLabel.textAlignment = NSTextAlignmentLeft;
            }
            
            if (Height < 64) {
                //内容自适应高度 默认最低60
                Height = 64;
            }
            
            //内容label
            _messageLabel.size = CGSizeMake(_contentSize.width - 2*_paddingLeft, Height);
            _messageLabel.y = _line.bottom + _spacing;
            _messageLabel.centerX = _contentSize.width / 2;
            
            
            _contentSize.height = _messageLabel.bottom;
        }
        
        self.size = CGSizeMake(_contentSize.width, _contentSize.height + _spacing);
        if (!title.length && !message.length) {
            self.size = CGSizeZero;
        }
    }
    return self;
}

@end
